#include "GUIManager.h"

template<> GUIManager* Ogre::Singleton<GUIManager>::ms_Singleton = 0;

GUIManager::GUIManager(Ogre::RenderWindow* mWindow) :
	mGUISystem(0),
	mGUIRenderer(0),
	mDefaultSheet(0),
	mRenderWindow(mWindow)
{
	// Setup GUI system -- initialize
	Ogre::SceneManager* mSceneMgr = Root::getSingletonPtr()->createSceneManager(ST_GENERIC, "DEF");
	mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mSceneMgr);
	mGUISystem = new CEGUI::System(mGUIRenderer);
	CEGUI::SchemeManager::getSingleton().loadScheme( (CEGUI::utf8*)"TaharezLookSkin.scheme");

	// Load scheme and set up defaults -- by constructor
	mGUISystem->setDefaultMouseCursor( (CEGUI::utf8*)"TaharezLook", (CEGUI::utf8*)"MouseArrow");
	mGUISystem->setDefaultFont((CEGUI::utf8*)"BlueHighway-12");
	CEGUI::MouseCursor::getSingleton().setImage("TaharezLook", "MouseArrow");
	CEGUI::MouseCursor::getSingleton().show( );

	createIntroGUI();
	createNetGUI();
}

GUIManager::~GUIManager()
{
	if(mGUISystem)
		delete mGUISystem;
	if(mGUIRenderer)
		delete mGUIRenderer;
	mGUISystem = 0;
	mGUIRenderer = 0;
}

void GUIManager::registerScene(Ogre::SceneManager* mSceneMgr)
{
	((CEGUI::OgreCEGUIRenderer*)mGUIRenderer)->setTargetSceneManager(mSceneMgr);
}

void GUIManager::setGUISheet(CEGUI::Window* sheet)
{
	mGUISystem->setGUISheet(sheet);
}

void GUIManager::createIntroGUI()
{
	// Setting up the background window sheet
	CEGUI::WindowManager *win = CEGUI::WindowManager::getSingletonPtr();
    CEGUI::Window *sheet = win->createWindow("DefaultGUISheet", "Intro/Sheet");
	
	// Create the Play button inside the default sheet
	CEGUI::Window *play = win->createWindow("TaharezLook/Button", "Intro/PlayButton");
    play->setText("Play");
    play->setSize(CEGUI::UVector2(CEGUI::UDim(0.10f, 0), CEGUI::UDim(0.04f, 0)));
	play->setPosition( CEGUI::UVector2(CEGUI::UDim(0.45f,0),CEGUI::UDim(0.7f,0)) );
	sheet->addChildWindow(play);
	
	// Create the Quit button inside the default sheet
	CEGUI::Window *quit = win->createWindow("TaharezLook/Button", "Intro/QuitButton");
    quit->setText("Quit");
    quit->setSize(CEGUI::UVector2(CEGUI::UDim(0.10f, 0), CEGUI::UDim(0.04f, 0)));
	quit->setPosition( CEGUI::UVector2(CEGUI::UDim(0.45f,0),CEGUI::UDim(0.8f,0)) );
	sheet->addChildWindow(quit);
}

void GUIManager::createNetGUI()
{
	// Setting up the background window sheet
	CEGUI::WindowManager *win = CEGUI::WindowManager::getSingletonPtr();
    CEGUI::Window *sheet = win->createWindow("DefaultGUISheet", "Net/Sheet");
	
	// Create the Create button inside the default sheet
	CEGUI::Window *create = win->createWindow("TaharezLook/Button", "Net/CreateButton");
    create->setText("Create");
    create->setSize(CEGUI::UVector2(CEGUI::UDim(0.10f, 0), CEGUI::UDim(0.04f, 0)));
	create->setPosition( CEGUI::UVector2(CEGUI::UDim(0.45f,0),CEGUI::UDim(0.5f,0)) );
	sheet->addChildWindow(create);
	
	// Create the Connect button inside the default sheet
	CEGUI::Window *connect = win->createWindow("TaharezLook/Button", "Net/ConnectButton");
    connect->setText("Connect");
    connect->setSize(CEGUI::UVector2(CEGUI::UDim(0.10f, 0), CEGUI::UDim(0.04f, 0)));
	connect->setPosition( CEGUI::UVector2(CEGUI::UDim(0.45f,0),CEGUI::UDim(0.6f,0)) );
	sheet->addChildWindow(connect);
	
	// Create the Start button inside the default sheet
	CEGUI::Window *start = win->createWindow("TaharezLook/Button", "Net/StartButton");
    start->setText("Start");
    start->setSize(CEGUI::UVector2(CEGUI::UDim(0.10f, 0), CEGUI::UDim(0.04f, 0)));
	start->setPosition( CEGUI::UVector2(CEGUI::UDim(0.45f,0),CEGUI::UDim(0.7f,0)) );
	sheet->addChildWindow(start);
	
	// Create the Quit button inside the default sheet
	CEGUI::Window *quit = win->createWindow("TaharezLook/Button", "Net/QuitButton");
    quit->setText("Quit");
    quit->setSize(CEGUI::UVector2(CEGUI::UDim(0.10f, 0), CEGUI::UDim(0.04f, 0)));
	quit->setPosition( CEGUI::UVector2(CEGUI::UDim(0.45f,0),CEGUI::UDim(0.8f,0)) );
	sheet->addChildWindow(quit);

	// Create the Textbox for displaying connection information
	CEGUI::Window *text = win->createWindow("TaharezLook/StaticText", "Net/Text");
	text->setText("Press button to proceed\n");
    text->setSize(CEGUI::UVector2(CEGUI::UDim(0.40f, 0), CEGUI::UDim(0.30f, 0)));
	text->setPosition( CEGUI::UVector2(CEGUI::UDim(0.30f,0),CEGUI::UDim(0.1f,0)) );
	sheet->addChildWindow(text);

	
	// Create the Editbox for taking input for IP
	CEGUI::Window *ip = win->createWindow("TaharezLook/Editbox", "Net/IP");
	ip->setText("");
    ip->setSize(CEGUI::UVector2(CEGUI::UDim(0.20f, 0), CEGUI::UDim(0.04f, 0)));
	ip->setPosition( CEGUI::UVector2(CEGUI::UDim(0.60f,0),CEGUI::UDim(0.6f,0)) );
	sheet->addChildWindow(ip);

	
	// Create the Editbox for taking input for message
	CEGUI::Window *msg = win->createWindow("TaharezLook/Editbox", "Net/Message");
	msg->setText("");
    msg->setSize(CEGUI::UVector2(CEGUI::UDim(0.40f, 0), CEGUI::UDim(0.04f, 0)));
	msg->setPosition( CEGUI::UVector2(CEGUI::UDim(0.30f,0),CEGUI::UDim(0.42f,0)) );
	sheet->addChildWindow(msg);

	
	// Create the button for sending messages
	CEGUI::Window *send = win->createWindow("TaharezLook/Button", "Net/Send");
	send->setText("Send");
    send->setSize(CEGUI::UVector2(CEGUI::UDim(0.10f, 0), CEGUI::UDim(0.04f, 0)));
	send->setPosition( CEGUI::UVector2(CEGUI::UDim(0.75f,0),CEGUI::UDim(0.42f,0)) );
	sheet->addChildWindow(send);
}